package com.yan_jiu_sheng.daily.AC;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;

/**
 * https://leetcode.cn/problems/baseball-game/description/?envType=daily-question&envId=2024-07-29
 * 自我AC通过
 *
 * @author yulongtian
 * @create 2024-07-29 10:10
 */
public class Test20240729 {
    public static void main(String[] args) {
        System.out.println(new Test20240729().calPoints(new String[]{"5", "2", "C", "D", "+"}));
    }

    public int calPoints(String[] operations) {
        Deque<Integer> stack = new LinkedList<>();
        HashSet<String> set = new HashSet<>(3);
        set.add("C");
        set.add("D");
        set.add("+");
        for (int i = 0; i < operations.length; i++) {
            String operation = operations[i];
            if (set.contains(operation)) {
                if (operation.equals("C")) {
                    stack.pop();
                } else if (operation.equals("D")) {
                    Integer pop1 = stack.peek();
                    stack.push(pop1 * 2);
                } else {// "+"
                    Integer pop1 = stack.pop();
                    Integer pop2 = stack.peek();
                    stack.push(pop1);
                    stack.push(pop1 + pop2);
                }
            } else {
                stack.addFirst(Integer.valueOf(operation));
            }
        }
        int res = 0;
        for (Integer i : stack) {
            res += i;
        }
        return res;
    }

}
